package com.vcredit.creditcat.time;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.common.threadpool.RequestLocalThread;
import com.vcredit.creditcat.manager.ProductApplyResultTimeManager;
import com.vcredit.creditcat.util.ConcurrentDateUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;


/**
 * 获取授信结果任务
 */
//@QuartzJob(name = "productApplyResultTime", cron = "0 0/1 * * * ?")
@JobHander(value="productApplyResultTime")
@Component
public class ProductApplyResultTime  extends IJobHandler  {

	private static Logger log = LoggerFactory.getLogger(ProductApplyResultTime.class);

	@Autowired
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;

	@Autowired
	private ProductApplyResultTimeManager productApplyResultTimeManager;

	@Override
	public ReturnT<String> execute(String... strings) throws Exception {
		log.info("获取授信结果任务执行了................" + ConcurrentDateUtil.formatDateTime(new Date()));
		RequestLocalThread.create();
		
		try {
			
			if(strings.length == 0){
				return new ReturnT<String>(ReturnT.FAIL_CODE,"productId不能为空");
			}
			for(int i=0;i<strings.length;i++){
				long productId = Long.parseLong(strings[i]);


				try {
					productApplyResultTimeManager.getProductApplyResult(productId);
				} catch (Exception e) {
					log.error("Exception occured", e);
				}

				//threadPoolTaskExecutor.execute(new CrawleThread(productId));
			}
					
		} catch (Exception e) {
			log.error(e.getMessage());
			return ReturnT.FAIL;
		}
		return ReturnT.SUCCESS;
	}
	

	class CrawleThread implements Runnable {
		private long productId;
		private Logger logger = LoggerFactory.getLogger(CrawleThread.class);

		public CrawleThread(long productId) {
			super();
			this.productId = productId;
		}

		public void run() {
			try {
				productApplyResultTimeManager.getProductApplyResult(productId);
			} catch (Exception e) {
				log.error("Exception occured", e);
			}
		}
	}

}
